#include "Elephant.h"
#include "EcoSystem.h"
#include "Vector2D.h"

Elephant::Elephant(EcoSystem *eco_system) :
Consumer(eco_system)
{
    target_tiger = NULL;
    set_alive();
    //!!!!!!!!!!!!!!!!!!!!
    set_max_hp(2000);
    set_hp(get_max_hp());
    //!!!!!!!!!!!!!!!!!!!!
    set_speed(0.4);
    set_init_speed(get_speed());
    set_life_span(LIFE_SPAN[get_species_id()] * (1 + max(min(0.2, EcoSystem::random_double() - 0.5), -0.2)));
    set_kicking_tiger(0);
}

Elephant::~Elephant() {}

const unsigned int Elephant::get_species_id() const
{
    return ELEPHANT_SPECIES_ID;
}

Gdiplus::Image* Elephant::get_entity_image() const
{
    static Gdiplus::Image *img = Gdiplus::Image::FromFile(L".\\images\\elephant.png"), *img1 = Gdiplus::Image::FromFile(L".\\images\\elephantwithtiger.png");
    if (get_kicking_tiger() <= 0)
        return img;
    else
        return img1;
}

Entity * Elephant::new_entity() const
{
    return new Elephant(eco_system);
}

int Elephant::get_cost_of_being_preyed() const
{
    return get_hp() *0.2;
}

int Elephant::get_gain_after_being_preyed() const
{
    return get_hp()*0.5;
}



int Elephant::get_kicking_tiger() const
{
    return kicking_tiger;
}

void Elephant::set_kicking_tiger(int times)
{
    kicking_tiger = times;
}
#include <iostream>
void Elephant::on_tick()
{
    if (get_kicking_tiger() > 0) set_kicking_tiger(get_kicking_tiger()-1);

    set_age(get_age() + 1);
    natural_growth();
    multiply();
    set_hp(get_hp() - get_speed()*get_speed() / 2);

    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    bool types[MAX_SPECIES_COUNT] = { 0 };
    types[TIGER_SPECIES_ID] = true;
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    if (target_tiger == NULL || !(target_tiger->is_alive()) || Vector2D::distance(get_position(), target_tiger->get_position()) > 300.0)
    {
        target_tiger = eco_system->find_entity(this, types);
        
    }

    double factor = get_environment_factor();

    if (get_kicking_tiger() <= 0)
    {
        if ((double)(get_age()) / get_life_span() > 0.10)
        {

            if (hungry() > 0.70 && target_tiger)
            {
                double dist = Vector2D::distance(get_position(), target_tiger->get_position());

                if (dist < 10)
                {
                    if (EcoSystem::random_double()*get_hp()*0.78 > EcoSystem::random_double()*target_tiger->get_hp())
                    {
                        target_tiger->set_dead();
                        target_tiger = NULL;
                        set_kicking_tiger(25);
                    }
                    else
                        set_dead();
                }
                else
                {
                    Vector2D direction = target_tiger->get_position() - get_position();
                    direction = 1.0 / direction.modulus()*direction;
                    set_position(get_position() + direction*get_init_speed()*2.0*factor);
                }
            }
            else
            {
                multiply();
                if (!prey(get_init_speed()*1.5*factor))
                    brownian_motion(get_init_speed()*factor);
            }
        }
        else
            if (hungry() > 0.70 || !prey(get_init_speed()*1.5*factor))
                brownian_motion(get_init_speed()*factor);

    }
    else{
        brownian_motion(get_init_speed()*0.5*factor);
    }
}

void Elephant::on_flush_memory()
{
    if (target_tiger != NULL && !target_tiger->is_alive())
        target_tiger = NULL;
}

